package com.mao.action.voucher;

import java.math.BigDecimal;
import java.util.Date;

import com.mao.action.BaseAction;
import com.mao.action.jsonvo.voucher.JsonVoVoucher;
import com.mao.model.organization.Organization;
import com.mao.model.product.Product;
import com.mao.model.voucher.DeliveryVoucher;
import com.mao.model.voucher.ProductDeliveryVoucher;
import com.mao.model.voucher.VoucherType;
import com.mao.model.voucher.VoucherVerityType;
import com.mao.model.warehouse.WareHouse;
import com.mao.service.common.ICommonService;
import com.mao.service.organization.IOrganizationService;
import com.mao.service.product.IProductService;
import com.mao.service.productstock.IProductStockService;
import com.mao.service.searchvoucher.ISearchVoucherService;
import com.mao.service.voucher.IDeliveryVoucherService;
import com.mao.service.voucher.IProductDeliveryVoucherService;
import com.mao.service.warehouse.IWareHouseService;
import com.mao.util.SessionHelper;

public class DeliveryVoucherAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private DeliveryVoucher dv;

	private IDeliveryVoucherService deliveryVoucherService;

	private IOrganizationService organizationService;

	private IWareHouseService wareHouseService;

	private IProductService productService;

	private IProductDeliveryVoucherService pdvService;

	private ICommonService commonService;

	private ISearchVoucherService searchVoucherService;

	private IProductStockService productStockService;

	private Organization organization;

	private WareHouse wareHouse;

	private String products;

	private ProductDeliveryVoucher pdv;

	private JsonVoVoucher returnVoucher;

	private String id;

	public String execute() {
		if (id != null) {
			dv = (DeliveryVoucher) searchVoucherService.getVoucherById(
					VoucherType.delivery.getVoucherClassName(),
					Long.valueOf(id));
		}
		return SUCCESS;
	}

	public String saveDeliveryVoucher() {
		organization = organizationService.getOrganizationById(dv.getOrg()
				.getId());
		wareHouse = wareHouseService
				.getWareHouseById(dv.getWareHouse().getId());
		String voucherNo = commonService.getVoucherNo(SessionHelper
				.getSessionUser().getId(), "XSD", DeliveryVoucher.class
				.getName());
		dv.setStore(SessionHelper.getSessionUser().getStore());
		dv.setOrg(organization);
		dv.setWareHouse(wareHouse);
		dv.setDatetime(new Date());
		dv.setVoucherNo(voucherNo);
		dv.setVoucherType(VoucherType.delivery);
		dv.setUser(SessionHelper.getSessionUser());
		deliveryVoucherService.saveDeliveryVoucher(dv);
		String[] prosArray = products.split(",");
		String[] pro = null;
		Product singlePro = null;
		Integer proAmount = 0;
		BigDecimal totalPrice = new BigDecimal(0);
		for (int i = 0; i < prosArray.length; i++) {
			pdv = new ProductDeliveryVoucher();
			pro = prosArray[i].split("_");
			singlePro = productService.getProductById(Long.valueOf(pro[0]));
			pdv.setProduct(singlePro);
			pdv.setUnitPrice(BigDecimal.valueOf(Double.valueOf(pro[1])));
			pdv.setSubtotal(BigDecimal.valueOf(Double.valueOf(pro[3])));
			pdv.setAmount(Integer.valueOf(pro[2]));
			// 商品总量计算
			proAmount = proAmount + pdv.getAmount();
			// 单据总价计算
			totalPrice = totalPrice.add(pdv.getSubtotal());
			pdv.setVoucher(dv);
			pdv.setStore(SessionHelper.getSessionUser().getStore());
			pdv.setVoucherType(VoucherType.delivery);
			pdvService.savePdv(pdv);
		}
		// 再存单据的商品总量
		dv.setProAmount(proAmount);
		// 再存单据的总价
		dv.setTotalPrice(totalPrice);
		deliveryVoucherService.saveDeliveryVoucher(dv);
		returnVoucher = new JsonVoVoucher();
		returnVoucher.setId(dv.getId());
		returnVoucher.setVoucherNo(voucherNo);
		return SUCCESS;
	}

	// 审核单据
	public String veriyVoucher() {
		organization = organizationService.getOrganizationById(dv.getOrg()
				.getId());
		wareHouse = wareHouseService
				.getWareHouseById(dv.getWareHouse().getId());
		DeliveryVoucher updateDv = (DeliveryVoucher) searchVoucherService
				.getVoucherById(VoucherType.delivery.getVoucherClassName(),
						Long.valueOf(id));
		updateDv.setTransactionAmount(dv.getTransactionAmount());
		updateDv.setReceiveAmount(dv.getReceiveAmount());
		updateDv.setNo(dv.getNo());
		updateDv.setNote(dv.getNote());
		updateDv.setOrg(organization);
		updateDv.setWareHouse(wareHouse);
		updateDv.setUpdatetime(new Date());
		updateDv.setUser(SessionHelper.getSessionUser());
		updateDv.setIsverity(VoucherVerityType.verityed);
		deliveryVoucherService.saveDeliveryVoucher(updateDv);
		String[] prosArray = products.split(",");
		String[] pro = null;
		Product singlePro = null;
		Integer proAmount = 0;
		BigDecimal totalPrice = new BigDecimal(0); 
		for (int i = 0; i < prosArray.length; i++) {
			pdv = new ProductDeliveryVoucher();
			pro = prosArray[i].split("_");
			singlePro = productService.getProductById(Long.valueOf(pro[0]));
			pdv.setProduct(singlePro);
			pdv.setUnitPrice(BigDecimal.valueOf(Double.valueOf(pro[1])));
			pdv.setAmount(Integer.valueOf(pro[2]));
			// 商品总量计算
			proAmount = proAmount + pdv.getAmount();
			pdv.setSubtotal(BigDecimal.valueOf(Double.valueOf(pro[3])));
			// 单据总价计算
			totalPrice = totalPrice.add(pdv.getSubtotal());
			pdv.setVoucher(updateDv);
			pdv.setStore(SessionHelper.getSessionUser().getStore());
			pdv.setVoucherType(VoucherType.delivery);
			pdvService.savePdv(pdv);
		}
		// 再存单据的商品总量
		updateDv.setProAmount(proAmount);
		// 再存单据的总价
		updateDv.setTotalPrice(totalPrice);
		deliveryVoucherService.saveDeliveryVoucher(updateDv);
		returnVoucher = new JsonVoVoucher();
		returnVoucher.setId(updateDv.getId());
		returnVoucher.setVoucherNo(updateDv.getVoucherNo());
		return SUCCESS;
	}

	public IOrganizationService getOrganizationService() {
		return organizationService;
	}

	public void setOrganizationService(IOrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	public IWareHouseService getWareHouseService() {
		return wareHouseService;
	}

	public void setWareHouseService(IWareHouseService wareHouseService) {
		this.wareHouseService = wareHouseService;
	}

	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	public WareHouse getWareHouse() {
		return wareHouse;
	}

	public void setWareHouse(WareHouse wareHouse) {
		this.wareHouse = wareHouse;
	}

	public String getProducts() {
		return products;
	}

	public void setProducts(String products) {
		this.products = products;
	}

	public IProductService getProductService() {
		return productService;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	public IDeliveryVoucherService getDeliveryVoucherService() {
		return deliveryVoucherService;
	}

	public void setDeliveryVoucherService(
			IDeliveryVoucherService deliveryVoucherService) {
		this.deliveryVoucherService = deliveryVoucherService;
	}

	public IProductDeliveryVoucherService getPdvService() {
		return pdvService;
	}

	public void setPdvService(IProductDeliveryVoucherService pdvService) {
		this.pdvService = pdvService;
	}

	public DeliveryVoucher getDv() {
		return dv;
	}

	public void setDv(DeliveryVoucher dv) {
		this.dv = dv;
	}

	public ProductDeliveryVoucher getPdv() {
		return pdv;
	}

	public void setPdv(ProductDeliveryVoucher pdv) {
		this.pdv = pdv;
	}

	public ICommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	public JsonVoVoucher getReturnVoucher() {
		return returnVoucher;
	}

	public void setReturnVoucher(JsonVoVoucher returnVoucher) {
		this.returnVoucher = returnVoucher;
	}

	public ISearchVoucherService getSearchVoucherService() {
		return searchVoucherService;
	}

	public void setSearchVoucherService(
			ISearchVoucherService searchVoucherService) {
		this.searchVoucherService = searchVoucherService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public IProductStockService getProductStockService() {
		return productStockService;
	}

	public void setProductStockService(IProductStockService productStockService) {
		this.productStockService = productStockService;
	}

}
